/****************************************************************************
*  Title: stimulus.sc
*  Author: Bryant Smith
*  Date: 04/16/2011
*  Description: stimulus test bench to read input from a file
				reads binary file "input" where the first byte represents the
				type of parity to use 0x01 is even parity, and 0x00 is odd
				parity.  After reading parity all bytes are sent over
				the c_channel to be encoded by the design.  The
				most significant bit on input must always be 0 since it
				is not used.
****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define IMG_ROWS 480
#define IMG_CLMS 640
#define IMG_HEADER 1078

import "c_queue";


behavior sendImageL(i_sender InQueueL)
{
	// File names
	char INPUT_BMP[] = "Stereo/leftX.bmp"; // where X is replaed by file number
	char OUTPUT_BMP[] = "output/outputX.bmp"; // output file name
	char imgBufCount[1];

	// image buffers these are currently dependent on the size and format of the image used
	char header_buffer[IMG_HEADER]; // 1078 byte header including raster data for BW grayscale 8 bit
	char row_buffer_1[IMG_CLMS];
	
	int row, a, wt_cnt, imgCount;
	
	void main(void) {	
	
	//File variable
	FILE * fptr;
	FILE * wptr;
	
	imgCount = 0;

		while(1)
		{		
			imgCount = imgCount ++;
			sprintf(imgBufCount, "%d", imgCount);
			
			// Determine Next image
			INPUT_BMP[11] = imgBufCount[0];
			OUTPUT_BMP[13] = imgBufCount[0];
			
			//********************
			// Start Processing //
			//********************			
			
			// Open files
			fptr = fopen(INPUT_BMP, "rb");
			wptr = fopen(OUTPUT_BMP, "w");
			
			
			if (fptr == NULL) {
				printf("Cannot open %s\n", INPUT_BMP);
				exit(0);
			}			
			printf("Stimulus program - sending image %s\n", INPUT_BMP);
			
			// read header info (1078 bytes including raster data)
			fread (header_buffer,1,IMG_HEADER,fptr);
			
			// Write header out the output image file before starting processing
			// should be able to do this in one step, didn't work????
			for(wt_cnt = 0; wt_cnt < IMG_HEADER; wt_cnt++)
			{
					fprintf(wptr,"%c",header_buffer[wt_cnt]);
			}
			fclose(wptr);
			
			// read file and send to queue one row at a time
			for(row = 0; row < IMG_ROWS; row++)
			{
				// Load buffer with a row of the image
				fread (row_buffer_1,1,(IMG_CLMS),fptr); 
				
				// send row over c_queue
				InQueueL.send(row_buffer_1, (IMG_CLMS));	
			}
			
			fclose(fptr);
			
			//wait LeftDone;
		}	
	}
};
	
	

behavior sendImageR(i_sender InQueueR)
{
	// File namesansi
	char INPUT_BMP[] = "Stereo/rightX.bmp"; // X is replaced for image number
	char DEBUG_BMP[] = "output8bpp.bmp";
	char imgBufCount[1];
	
	// image buffers these are currently dependent on the size and format of the image used
	char header_buffer[IMG_HEADER]; // 1078 byte header including raster data for BW grayscale 8 bit
	char row_buffer_1[IMG_CLMS];
	
	int row, a, wt_cnt, imgCount;
	
	void main(void) {	
	
	//File variable
	FILE * fptr;
	FILE * wptrdebug;
	
	imgCount = 0;

		while(1)
		{		
			imgCount = imgCount ++;
			sprintf(imgBufCount, "%d", imgCount);
			
			// Determine Next image
			INPUT_BMP[12] = imgBufCount[0];
			
			//********************
			// Start Processing //
			//********************			
			
			// Open files
			fptr = fopen(INPUT_BMP, "rb");
			wptrdebug = fopen(DEBUG_BMP, "w"); 	
			
			
			if (fptr == NULL) {
				printf("Cannot open %s\n", INPUT_BMP);
				exit(0);
			}			
			printf("Stimulus program - sending image %s\n", INPUT_BMP);
			
			// read header info (1078 bytes including raster data)
			fread (header_buffer,1,IMG_HEADER,fptr);
			
			// Write header out the output image file before starting processing
			for(wt_cnt = 0; wt_cnt < IMG_HEADER; wt_cnt++)
			{
					fprintf(wptrdebug, "%c",header_buffer[wt_cnt]);
			}
			fclose(wptrdebug);
			
			// read file and send to queue one row at a time
			for(row = 0; row < IMG_ROWS; row++)
			{
				// Load buffer with a row of the image
				fread (row_buffer_1,1,(IMG_CLMS),fptr); 
				
				// send row over c_queue
				InQueueR.send(row_buffer_1, (IMG_CLMS));	
			}
			
			fclose(fptr);
			
			//wait RightDone;
		}	
	}
};

behavior stimulus(i_sender InQueueL, i_sender InQueueR)
{
	sendImageL simL(InQueueL);
	sendImageR simR(InQueueR);
	
	void main() {
		par{
			simL.main();
			simR.main();
		}
	}
};
